/**
 * 用户管理API
 * 处理用户CRUD、用户状态管理等相关接口
 */

import request from '@/utils/request'

// 请求和响应数据类型定义
interface User {
  id: string
  username: string
  realName: string
  role: 'ADMIN' | 'SCHOOL_ADMIN' | 'TEACHER' | 'PARENT'
  phone?: string
  email?: string
  avatar?: string
  status: 'ACTIVE' | 'INACTIVE'
  createdTime: string
  lastLoginTime?: string
  schoolId?: string
}

interface UserCreateRequest {
  username: string
  password: string
  realName: string
  role: 'ADMIN' | 'SCHOOL_ADMIN' | 'TEACHER' | 'PARENT'
  phone?: string
  email?: string
  schoolId?: string
  schoolName?: string
}

interface UserUpdateRequest {
  username?: string
  realName?: string
  phone?: string
  email?: string
  avatar?: string
  status?: 'ACTIVE' | 'INACTIVE'
}

interface UserListParams {
  role?: 'ADMIN' | 'SCHOOL_ADMIN' | 'TEACHER' | 'PARENT'
  status?: 'ACTIVE' | 'INACTIVE'
  page?: number
  pageSize?: number
  keyword?: string
}

interface ApiResponse<T> {
  code: number
  message: string
  data: T
  timestamp: string
}

interface PageResponse<T> {
  list: T[]
  total: number
  page: number
  pageSize: number
}

/**
 * 用户管理API类
 */
class UserAPI {
  /**
   * 获取用户列表
   */
  async getUsers(params: UserListParams = {}): Promise<ApiResponse<PageResponse<User>>> {
    const queryParams: Record<string, any> = {
      page: params.page || 1,
      pageSize: params.pageSize || 20
    }

    if (params.role) queryParams.role = params.role
    if (params.status) queryParams.status = params.status
    if (params.keyword) queryParams.keyword = params.keyword

    return request.get<PageResponse<User>>('/api/users', queryParams, {
      showLoading: true,
      loadingText: '获取用户列表...',
      showError: true
    })
  }

  /**
   * 根据ID获取用户信息
   */
  async getUserById(userId: string): Promise<ApiResponse<User>> {
    return request.get<User>(`/api/users/${userId}`, {}, {
      showLoading: true,
      loadingText: '获取用户信息...',
      showError: true
    })
  }

  /**
   * 创建用户
   */
  async createUser(userData: UserCreateRequest): Promise<ApiResponse<User>> {
    return request.post<User>('/api/users', userData, {
      showLoading: true,
      loadingText: '创建用户...',
      showError: true
    })
  }

  /**
   * 更新用户信息
   */
  async updateUser(userId: string, userData: UserUpdateRequest): Promise<ApiResponse<User>> {
    return request.put<User>(`/api/users/${userId}`, userData, {
      showLoading: true,
      loadingText: '更新用户信息...',
      showError: true
    })
  }

  /**
   * 删除用户
   */
  async deleteUser(userId: string): Promise<ApiResponse<void>> {
    return request.request<void>({
      url: `/api/users/${userId}`,
      method: 'DELETE',
      showLoading: true,
      loadingText: '删除用户...',
      showError: true
    })
  }

  /**
   * 重置用户密码
   */
  async resetUserPassword(userId: string, newPassword: string): Promise<ApiResponse<void>> {
    return request.put<void>(`/api/users/${userId}/reset-password`,
      { newPassword },
      {
        showLoading: true,
        loadingText: '重置密码...',
        showError: true
      }
    )
  }

  /**
   * 激活用户
   */
  async activateUser(userId: string): Promise<ApiResponse<void>> {
    return request.put<void>(`/api/users/${userId}/activate`, {}, {
      showLoading: true,
      loadingText: '激活用户...',
      showError: true
    })
  }

  /**
   * 禁用用户
   */
  async deactivateUser(userId: string): Promise<ApiResponse<void>> {
    return request.put<void>(`/api/users/${userId}/deactivate`, {}, {
      showLoading: true,
      loadingText: '禁用用户...',
      showError: true
    })
  }

  /**
   * 根据角色获取用户列表
   */
  async getUsersByRole(role: string): Promise<ApiResponse<User[]>> {
    return request.get<User[]>(`/api/users/role/${role}`, {}, {
      showLoading: true,
      loadingText: '获取用户列表...',
      showError: true
    })
  }

  /**
   * 根据状态获取用户列表
   */
  async getUsersByStatus(status: string): Promise<ApiResponse<User[]>> {
    return request.get<User[]>(`/api/users/status/${status}`, {}, {
      showLoading: true,
      loadingText: '获取用户列表...',
      showError: true
    })
  }

  /**
   * 获取管理员用户列表
   */
  async getAdminUsers(): Promise<ApiResponse<User[]>> {
    return request.get<User[]>('/api/users/admins', {}, {
      showLoading: true,
      loadingText: '获取管理员列表...',
      showError: true
    })
  }

  /**
   * 获取教师用户列表
   */
  async getTeacherUsers(): Promise<ApiResponse<User[]>> {
    return request.get<User[]>('/api/users/teachers', {}, {
      showLoading: true,
      loadingText: '获取教师列表...',
      showError: true
    })
  }

  /**
   * 获取家长用户列表
   */
  async getParentUsers(): Promise<ApiResponse<User[]>> {
    return request.get<User[]>('/api/users/parents', {}, {
      showLoading: true,
      loadingText: '获取家长列表...',
      showError: true
    })
  }

  /**
   * 批量操作用户状态
   */
  async batchUpdateUserStatus(userIds: string[], status: 'ACTIVE' | 'INACTIVE'): Promise<ApiResponse<void>> {
    return request.put<void>('/api/users/batch-status', null, {
      params: { userIds, status },
      showLoading: true,
      loadingText: '批量更新用户状态...',
      showError: true
    })
  }

  /**
   * 批量删除用户
   */
  async batchDeleteUsers(userIds: string[]): Promise<ApiResponse<void>> {
    return request.delete<void>('/api/users/batch', {
      userIds
    }, {
      showLoading: true,
      loadingText: '批量删除用户...',
      showError: true
    })
  }
}

// 创建API实例
export const userAPI = new UserAPI()

// 导出类型
export type {
  User,
  UserCreateRequest,
  UserUpdateRequest,
  UserListParams,
  PageResponse
}